package gui;

import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.UUID;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import myz.date.gui.CalendarPad;
import myz.date.gui.NumberLenghtLimitedDmt;

import org.apache.commons.io.FilenameUtils;

import db.domain.Student;
import db.sqldao.StudentSqlDao;

/**
 * 校园卡基本信息界面（添加，修改，删除，查询）， 管理员管理，系统设置
 * 
 * @author 冀鲁
 * @date 2011-06-29
 * @version 1.0.0.0
 * 
 */
@SuppressWarnings("serial")
public class StudentManagerForm extends JDialog implements MouseListener,
		ActionListener {
	private DateFormat f1 = new SimpleDateFormat("yyyy-MM-dd");// 时间对象
	private JLabel lblMoney;// 余额
	private JTextField txtMoney;
	private JLabel lblLose;// 丢失
	private JRadioButton rdbLose[];
	private JPanel jp;
	private JLabel lblNo;// 学号
	private JTextField txtNo;// 学号
	private JLabel lblName;// 姓名
	private JTextField txtName;// 姓名
	private JLabel lblSex;// 性别
	private JRadioButton rdbSex[];// 性别
	private JLabel lblMinZu;// 民族
	private JComboBox cboMinZu;// 民族
	String strMinzu = "==请选择==,汉族,蒙古族,回族,藏族,维吾尔族,苗族,彝族,朝鲜族,满族,瑶族,黎族,高山族,壮族,布依族,侗族,白族,"
			+ "哈萨克族,哈尼族,傣族,傈僳族,侗族,东乡族,纳西族,拉古族,水族,景颇族,柯尔克孜族,土族,塔吉克族,乌孜别克族,塔塔尔族,鄂温克族,"
			+ "保安族,羌族,撒拉族,俄罗斯族,锡伯族,裕固族,鄂伦春族,土家族,畲族,达斡尔族,么佬族,布朗族,讫佬族,阿昌族,普米族,怒族,德昂族,"
			+ "京族,独龙族,赫哲族,门巴族,毛南族,哈巴族,基诺族,其它";
	private JLabel lblPhone;// 联系电话
	private JTextField txtPhone;// 联系电话
	private JLabel lblBirthday;// 出生日期
	private JTextField txtBirthday;// 出生日期
	private JLabel lblAddress;
	private JTextField txtAddress;// 联系地址
	private JLabel lblHappy;
	private JCheckBox chkHappy[];// 个人爱好
	private JLabel lblJianLi;
	private JTextArea txtJianLi;// 个人简历
	private JLabel lblPicture;// 个人照片
	private JButton btnUpload;// 上传照片
	private JButton btnDelete;// 删除照片
	private JButton btnManager;// 多重功能
	private JButton btnBack;// 返回
	private JLabel lblPicture1;// 系统图片
	private String type;

	public StudentManagerForm(String type, Student student) {
		// 添加一些组件，添加到jp上
		int width = 600, height = 450;
		int x0 = 15, y0 = 50, w0 = 65, h0 = 20;
		int w1 = 120, ww = 30, hh = 10;

		this.type = type;
		String str = "";
		if ("add".equalsIgnoreCase(type)) {
			str = "添加";
		} else if ("modify".equalsIgnoreCase(type)) {
			str = "修改";
		} else if ("delete".equalsIgnoreCase(type)) {
			str = "删除";
		} else if ("query".equalsIgnoreCase(type)) {
			str = "查询";
		}
		this.setTitle(str + "学生基本信息界面");
		this.setLayout(null);
		jp = new JPanel(null);// 使用坐标布局

		TitledBorder tb = new TitledBorder("学生基本信息");// 标题
		tb.setTitleColor(Color.red);// 标题颜色
		// 标题字体
		tb.setTitleFont(new Font("黑体", Font.BOLD, 20));
		// 边框颜色和宽度
		tb.setBorder(new LineBorder(Color.blue, 2, false));// false直角true 圆角
		jp.setBorder(tb);
		jp.setBounds(0, 0, width, height);
		this.add(jp);
		// 第一行
		lblMoney = new JLabel("余额：");
		lblMoney.setHorizontalAlignment(JLabel.RIGHT);// 对齐方式
		// lblNo.setBorder(new LineBorder(Color.red, 1, false));
		lblMoney.setBounds(x0, y0 - 30, w0, h0);
		jp.add(lblMoney);
		txtMoney = new JTextField();
		txtMoney.setBounds(x0 + w0, y0 - 30, w1, h0);
		txtMoney.setBorder(new LineBorder(Color.black, 1, true));
		txtMoney.setDocument(new NumberLenghtLimitedDmt(5));
		txtMoney.setEnabled(false);// 文本框不可编辑
		jp.add(txtMoney);

		// 第二行
		lblLose = new JLabel("丢失：");
		lblLose.setHorizontalAlignment(JLabel.RIGHT);
		// lblSex.setBorder(new LineBorder(Color.red, 1, false));
		lblLose.setBounds(x0 + 215, y0 - 30, w0, h0);
		jp.add(lblLose);

		String[] strLose = { "是", "否" };
		rdbLose = new JRadioButton[strLose.length];
		ButtonGroup bg1 = new ButtonGroup();// 按钮组（和单选有关）
		for (int i = 0; i < strLose.length; i++) {
			rdbLose[i] = new JRadioButton(strLose[i]);
			rdbLose[i].setBorder(new LineBorder(Color.red, 1, false));
			bg1.add(rdbLose[i]);
			jp.add(rdbLose[i]);
			rdbLose[i].setBounds(lblLose.getX() + w0 + 45 * i, lblLose.getY(),
					45, h0);
		}
		rdbLose[0].setSelected(true);// 被选择

		// 第一行
		lblNo = new JLabel("学号：");
		lblNo.setHorizontalAlignment(JLabel.RIGHT);// 对齐方式
		// lblNo.setBorder(new LineBorder(Color.red, 1, false));
		lblNo.setBounds(x0, y0, w0, h0);
		jp.add(lblNo);

		txtNo = new JTextField();
		txtNo.setBounds(x0 + w0, y0, w1, h0);
		txtNo.setBorder(new LineBorder(Color.black, 1, false));
		// 只能输入数字，最多五位数
		txtNo.setDocument(new NumberLenghtLimitedDmt(5));
		txtNo.setEnabled(false);// 文本框不可编辑
		jp.add(txtNo);

		lblName = new JLabel("姓名：");
		lblName.setHorizontalAlignment(JLabel.RIGHT);
		// lblName.setBorder(new LineBorder(Color.red, 1, false));
		lblName.setBounds(txtNo.getX() + w1 + ww, y0, w0, h0);
		jp.add(lblName);

		txtName = new JTextField();
		txtName.setBounds(lblName.getX() + w0, y0, w1, h0);
		txtName.setBorder(new LineBorder(Color.red, 1, false));
		jp.add(txtName);

		// 第二行
		lblSex = new JLabel("性别：");
		lblSex.setHorizontalAlignment(JLabel.RIGHT);
		// lblSex.setBorder(new LineBorder(Color.red, 1, false));
		lblSex.setBounds(x0, lblNo.getY() + h0 + hh, w0, h0);
		jp.add(lblSex);

		String[] strSex = { "男", "女" };
		rdbSex = new JRadioButton[strSex.length];
		ButtonGroup bg = new ButtonGroup();// 按钮组（和单选有关）
		for (int i = 0; i < strSex.length; i++) {
			rdbSex[i] = new JRadioButton(strSex[i]);
			rdbSex[i].setBorder(new LineBorder(Color.red, 1, false));
			bg.add(rdbSex[i]);
			jp.add(rdbSex[i]);
			rdbSex[i].setBounds(lblSex.getX() + w0 + 45 * i, lblSex.getY(), 45,
					h0);
		}
		rdbSex[0].setSelected(true);// 被选择

		lblMinZu = new JLabel("民族：");
		lblMinZu.setHorizontalAlignment(JLabel.RIGHT);
		// lblMinZu.setBorder(new LineBorder(Color.red, 1, false));
		lblMinZu.setBounds(lblName.getX(), lblSex.getY(), w0, h0);
		jp.add(lblMinZu);

		// 将字符串分割成字符串数组split
		String[] minZuArray = strMinzu.split(",");// 把字符串转化成字符串数组
		cboMinZu = new JComboBox();
		for (int i = 0; i < minZuArray.length; i++) {
			cboMinZu.addItem(minZuArray[i]);
		}
		cboMinZu.setBounds(txtName.getX(), lblMinZu.getY(), w1, h0);
		cboMinZu.setBorder(new LineBorder(Color.red, 1, false));
		jp.add(cboMinZu);

		// 第三行
		lblPhone = new JLabel("联系电话：");
		lblPhone.setHorizontalAlignment(JLabel.RIGHT);
		// lblPhone.setBorder(new LineBorder(Color.red, 1, false));
		lblPhone.setBounds(x0, lblSex.getY() + h0 + hh, w0, h0);
		jp.add(lblPhone);

		txtPhone = new JTextField();
		txtPhone.setBounds(txtNo.getX(), lblPhone.getY(), w1, h0);
		txtPhone.setBorder(new LineBorder(Color.black, 1, false));
		jp.add(txtPhone);

		lblBirthday = new JLabel("出生日期：");
		lblBirthday.setHorizontalAlignment(JLabel.RIGHT);
		lblBirthday.setBounds(lblName.getX(), lblPhone.getY(), w0, h0);
		jp.add(lblBirthday);

		txtBirthday = new JTextField();
		txtBirthday.setBounds(txtName.getX(), lblPhone.getY(), w1, h0);
		txtBirthday.setBorder(new LineBorder(Color.red, 1, false));
		txtBirthday.setEditable(false);// 不可编辑（文本框中的字体带有颜色的情况）
		txtBirthday.addMouseListener(this);
		txtBirthday.setToolTipText("请点击文本框选择日期");// 显示提示信息
		if ("add".equalsIgnoreCase(type) || "modify".equalsIgnoreCase(type)) {
			jp.add(txtBirthday);
		}
		// 第四行
		lblAddress = new JLabel("联系地址：");
		lblAddress.setHorizontalAlignment(JLabel.RIGHT);
		lblAddress.setBounds(x0, lblPhone.getY() + h0 + hh, w0, h0);
		jp.add(lblAddress);

		txtAddress = new JTextField();
		txtAddress.setBounds(txtNo.getX(), lblAddress.getY(),
				w1 + ww + w0 + w1, h0);
		txtAddress.setBorder(new LineBorder(Color.red, 1, false));
		jp.add(txtAddress);

		// 第五行
		lblHappy = new JLabel("个人爱好：");
		lblHappy.setHorizontalAlignment(JLabel.RIGHT);
		lblHappy.setBounds(x0, lblAddress.getY() + h0 + hh, w0, h0);
		jp.add(lblHappy);

		String strHappy[] = { "音乐", "上网", "游戏", "交朋友" };
		chkHappy = new JCheckBox[strHappy.length];
		for (int i = 0; i < strHappy.length; i++) {
			chkHappy[i] = new JCheckBox(strHappy[i]);
			chkHappy[i].setBounds(txtNo.getX() + 80 * i, lblHappy.getY(), 80,
					h0);
			jp.add(chkHappy[i]);
		}

		// 第六行
		lblJianLi = new JLabel("个人简历：");
		lblJianLi.setHorizontalAlignment(JLabel.RIGHT);
		lblJianLi.setBounds(x0, lblHappy.getY() + h0 + hh, w0, h0);
		jp.add(lblJianLi);

		txtJianLi = new JTextArea();
		txtJianLi.setLineWrap(true);// 自动换行
		/* 带有滚动条的面板容器 */
		JScrollPane jsp = new JScrollPane(txtJianLi,/* 组件对象 */
		JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,/* 垂直滚动条，显示 */
		JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);/* 水平滚动条，不显示 */
		jsp.setBounds(txtNo.getX(), lblJianLi.getY(), w1 + ww + w0 + w1, 110);
		jp.add(jsp);

		// 第七行
		btnManager = new JButton(str);
		btnManager.setBounds(130, jsp.getY() + jsp.getHeight() + hh, 80, 30);
		btnManager.addActionListener(this);
		if ("query".equalsIgnoreCase(type)) {
			btnManager.setVisible(false);
		}
		jp.add(btnManager);

		btnBack = new JButton("返回");
		btnBack.setBounds(btnManager.getX() + btnManager.getWidth() + 50,
				btnManager.getY(), btnManager.getWidth(), btnManager
						.getHeight());
		btnBack.addActionListener(this);
		jp.add(btnBack);

		// 设置图片的位置
		lblPicture = new JLabel(
				"<html>		未<br>		上<br>		传<br>		皮<br>		肤<br></html>");
		lblPicture.setBorder(new LineBorder(Color.blue, 5, true));
		lblPicture.setBounds(x0 + w0 + w1 + ww + w0 + w1 + ww, y0, 90, 120);
		jp.add(lblPicture);

		btnUpload = new JButton("上传照片");
		btnUpload.setBounds(lblPicture.getX(), lblPicture.getY()
				+ lblPicture.getHeight() + hh, 90, 30);
		if ("add".equalsIgnoreCase(type) || "delete".equalsIgnoreCase(type)) {
			btnUpload.setEnabled(false);// 按钮不可用
		}
		btnUpload.addActionListener(this);
		jp.add(btnUpload);

		btnDelete = new JButton("删除照片");
		btnDelete.setBounds(btnUpload.getX(), btnUpload.getY()
				+ btnUpload.getHeight() + hh, btnUpload.getWidth(), btnUpload
				.getHeight());
		if ("add".equalsIgnoreCase(type) || "delete".equalsIgnoreCase(type)) {
			btnDelete.setEnabled(false);// 按钮不可用
		}
		btnDelete.addActionListener(this);
		jp.add(btnDelete);

		this.setModal(true);// 窗口独占模式（JDialog独有）
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		// 匿名内部类
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0) {
				closeWindow();
			}
		});
		// 设置图片的位置
		lblPicture1 = new JLabel("<html>　　系<br>　　统<br>　　图<br>　　片<br></html>");
		// lblPicture.setBorder(new LineBorder(Color.blue, 5, true));
		lblPicture1.setBounds(0, 0, width, height);// 设置图片位置
		displayPictur displayPictur = new displayPictur(3, lblPicture1);
		jp.add(lblPicture1);

		this.setSize(width, height);
		this.setResizable(false);
		if (!"add".equalsIgnoreCase(type)) {
			displayStudent(student);
		}
		if ("query".equalsIgnoreCase(type) || "delete".equalsIgnoreCase(type)) {
			setEnableForm(false);
		}
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}

	/**
	 * 设置窗口上的组件是否可编辑
	 * 
	 * @param isEdit
	 *            false 不可编辑 true可编辑
	 */
	private void setEnableForm(boolean isEdit) {
		txtMoney.setEditable(isEdit);// 余额
		txtNo.setEnabled(isEdit);// 文本框不可编辑
		txtName.setEditable(isEdit);// 姓名
		for (int i = 0; i < rdbSex.length; i++) {
			rdbSex[i].setEnabled(isEdit);
		}
		cboMinZu.setEnabled(isEdit);// 民族
		txtPhone.setEditable(isEdit);
		txtAddress.setEditable(isEdit);
		for (int i = 0; i < chkHappy.length; i++) {
			chkHappy[i].setEnabled(isEdit);
		}
		txtJianLi.setEditable(isEdit);

	}

	protected void closeWindow() {
		// 关闭窗口
		this.dispose();// 释放本窗口资源
	}

	public void mouseClicked(MouseEvent e) {
		if (e.getButton() == MouseEvent.BUTTON1/* 左键 */) {
			// 获取鼠标的位置
			double x = e.getLocationOnScreen().getX();
			double y = e.getLocationOnScreen().getY();
			CalendarPad cp = new CalendarPad(2010, 4, 27, x, y);
			txtBirthday.setText(cp.toString());
		}
	}

	public void mouseEntered(MouseEvent e) {

	}

	public void mouseExited(MouseEvent e) {

	}

	public void mousePressed(MouseEvent e) {

	}

	public void mouseReleased(MouseEvent e) {

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnManager) {
			if ("add".equalsIgnoreCase(type)) {// 添加
				addStudent();
			} else if ("modify".equalsIgnoreCase(type)) {// 修改
				modifyStudent();
			} else if ("delete".equalsIgnoreCase(type)) {// 删除
				deleteStudent();
			}

		} else if (e.getSource() == btnBack) { // 返回
			this.dispose();// 释放本窗口资源
		} else if (e.getSource() == btnUpload) {
			// (1)弹出选举文件窗口
			FileDialog fd = new FileDialog(this, "打开文件", FileDialog.LOAD);
			fd.setFile("*.gif");
			fd.setVisible(true);
			// (2)选择文件（图片）
			String dir = fd.getDirectory();// 获取目录
			String fileName = fd.getFile();// 获取文件
			if (dir != null && fileName != null) {
				Savepoint(dir + "\\" + fileName);
			}
		} else if (e.getSource() == btnDelete) {
			int yes = JOptionPane.showConfirmDialog(this, "您确认删除图片吗？", "删除提示",
					JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
			if (yes == JOptionPane.YES_OPTION) {
				// 根据用户no，查询的用户信息
				StudentSqlDao sDao = new StudentSqlDao();
				Student s = sDao.getStudent("where no=" + txtNo.getText()).get(
						0);
				String path = s.getPhotoPath();
				if (path != null) {
					File f = new File(path);
					if (f.exists()) {
						f.delete();// 删除文件
					}

				}
				// 删除数据库中图片路径字段设置为空
				sDao.updateUserPhotoPath(Integer.parseInt(txtNo.getText()),
						null);
				lblPicture.setIcon(null);
			}
		}
	}

	/**
	 * 删除
	 */
	private void deleteStudent() {
		int yes = JOptionPane.showConfirmDialog(this, "您确认删除该用户吗？", "删除提示",
				JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		if (yes == JOptionPane.YES_OPTION) {
			StudentSqlDao sDao = new StudentSqlDao();
			Student s = sDao.getStudent("where no=" + txtNo.getText()).get(0);
			String path = s.getPhotoPath();
			if (path != null) {
				File f = new File(path);
				if (f.exists()) {
					f.delete();// 删除文件
				}

			}
			boolean flag = sDao
					.deleteStudent(Integer.parseInt(txtNo.getText()));
			if (flag) {
				this.dispose();
			} else {
				JOptionPane.showMessageDialog(this, "对不起【删除】失败！");
			}
		}

	}

	/**
	 * 添加
	 */
	private void addStudent() {
		// (1)验证某些属性释是否为空
		if (validateForm()) {
			return;
		}
		// (2)从界面上获取学生信息封装成对象
		Student s = getStudentForm();
		// (3)先判断姓名是否存在，若存在不能进行添加
		StudentSqlDao sDao = new StudentSqlDao();
		ArrayList<Student> al = sDao.getStudent("where name='"
				+ txtName.getText().trim() + "'");// binary是二进制转换用来区分大小写
		if (al.size() > 0) {
			JOptionPane.showMessageDialog(this, "对不起，【姓名】已经存在，请重新输入！");
			// 文本框中的内容自动选中
			txtName.setSelectionStart(0);
			txtName.setSelectionEnd(txtName.getText().length());
			txtName.requestFocus(true);
			return;
		}

		// (4)使用jdbc技术，将对象信息保存到数据库中

		boolean flag = sDao.addStudent(s);
		if (flag) {
			JOptionPane.showMessageDialog(this, "恭喜你【添加学生信息】 成功！");
			// 清空界面上的信息
			clearForm();
		} else {
			JOptionPane.showMessageDialog(this, "对不起【添加学生信息】 失败！");
		}

	}

	/**
	 * 修改
	 */
	private void modifyStudent() {
		// (1)验证某些属性释是否为空
		if (validateForm()) {
			return;
		}
		// (2)从界面上获取学生信息封装成对象
		Student s = getStudentForm();
		// (3)先判断姓名是否存在，若存在不能进行添加
		StudentSqlDao sDao = new StudentSqlDao();
		ArrayList<Student> al = sDao.getStudent("where name='"
				+ txtName.getText().trim() + "' and no<>" + s.getNo());// binary是二进制转换用来区分大小写
		if (al.size() > 0) {
			JOptionPane.showMessageDialog(this, "对不起，【姓名】已经存在，请重新输入！");
			// 文本框中的内容自动选中
			txtName.setSelectionStart(0);
			txtName.setSelectionEnd(txtName.getText().length());
			txtName.requestFocus(true);
			return;
		}

		// (4)使用jdbc技术，将对象信息保存到数据库中

		boolean flag = sDao.updateStudent(s);
		if (flag) {
			JOptionPane.showMessageDialog(this, "恭喜你【修改学生信息】 成功！");
		} else {
			JOptionPane.showMessageDialog(this, "对不起【修改学生信息】 失败！");
		}
	}

	/**
	 * 清空界面上的组件内容
	 */
	private void clearForm() {
		txtMoney.setText("");// 余额
		rdbLose[1].setSelected(true);// 默认“男”
		txtName.setText("");
		rdbSex[0].setSelected(true);// 默认“男”
		cboMinZu.setSelectedIndex(0);// 默认为第一项
		txtPhone.setText("");// 电话
		txtBirthday.setText("");// 生日
		txtAddress.setText("");// 地址
		txtJianLi.setText("");// 简历
		for (int i = 0; i < chkHappy.length; i++) {// 爱好
			chkHappy[i].setSelected(false);
		}
	}

	/**
	 * 从界面上获取学生信息封装成对象
	 * 
	 * @return
	 */
	private Student getStudentForm() {
		Student s = new Student();
		int money = 0;
		if (!"add".equals(type)) {
			money = Integer.parseInt(txtMoney.getText());// 余额
		}
		String lose = null; // 丢失
		for (int i = 0; i < rdbLose.length; i++) {
			if (rdbLose[i].isSelected()) {
				lose = rdbLose[i].getText();
				break;
			}
		}
		s.setLose(lose);
		// System.out.println(lose);
		s.setMoney(money);
		if (txtNo.getText().length() > 0) {
			s.setNo(Integer.parseInt(txtNo.getText()));
		} else {
			s.setNo(0);
		}
		s.setName(this.txtName.getText().trim());
		String sex = null;
		for (int i = 0; i < rdbSex.length; i++) {
			if (rdbSex[i].isSelected()) {
				sex = rdbSex[i].getText();
				break;
			}
		}
		s.setSex(sex);
		s.setNation(this.cboMinZu.getSelectedItem().toString());
		s.setTelephone(this.txtPhone.getText().trim());
		try {
			s.setBirthday(f1.parse(this.txtBirthday.getText()));
			// System.out.print(f1.parse(this.txtBirthday.getText()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		s.setAddress(this.txtAddress.getText().trim());
		String happy = null;
		for (int i = 0; i < chkHappy.length; i++) {
			if (chkHappy[i].isSelected()) {
				if ("".equals(happy)) {// 第一不加，号
					happy = chkHappy[i].getText();
				} else {
					happy = happy + "," + chkHappy[i].getText();
				}
			}
		}
		s.setHappy(happy);
		s.setReume(this.txtJianLi.getText().trim());

		return s;
	}

	/**
	 * 判断某些组件内容不能为空
	 * 
	 * @return 如果为空返回true,否则返回false
	 */
	private boolean validateForm() {
		// 姓名
		String name = this.txtName.getText().trim();// .taim()去除两端的空格
		if (name.length() == 0) {
			JOptionPane.showMessageDialog(this, "对不起【姓名】不能为空！");
			txtName.requestFocus(true);// 设置焦点
			return true;
		}
		// 丢失
		boolean flag1 = false;
		for (int i = 0; i < rdbLose.length; i++) {
			if (rdbLose[i].isSelected()) {
				flag1 = true;
				break;
			}
		}

		// 性别
		boolean flag = false;
		for (int i = 0; i < rdbSex.length; i++) {
			if (rdbSex[i].isSelected()) {
				flag = true;
				break;
			}
		}
		if (flag == false) {
			JOptionPane.showMessageDialog(this, "对不起【性别】不能为空！");
			return true;
		}

		// 民族
		int index = cboMinZu.getSelectedIndex();// 获取当前索引值
		if (index == 0) {
			JOptionPane.showMessageDialog(this, "对不起【民族】不能为空！");
			cboMinZu.requestFocus(true);
			return true;
		}

		// 出生日期
		String birthday = txtBirthday.getText();
		if (birthday.length() == 0) {
			JOptionPane.showMessageDialog(this, "对不起【出生日期】不能为空！");
			return true;
		}

		// 地址
		String address = txtAddress.getText();
		if (address.length() == 0) {
			JOptionPane.showMessageDialog(this, "对不起【联系地址】不能为空！");
			txtAddress.requestFocus(true);// 设置焦点
			return true;
		}

		return false;
	}

	/**
	 * 将学生信息显示到窗口的组件上
	 * 
	 * @param student
	 */
	private void displayStudent(Student student) {
		txtMoney.setText(Integer.toString(student.getMoney()));// 余额
		String lose = student.getLose();// 丢失
		for (int i = 0; i < rdbLose.length; i++) {
			if (lose.equals(rdbLose[i].getText())) {
				rdbLose[i].setSelected(true);
				break;
			}
		}
		txtNo.setText(Integer.toString(student.getNo()));
		txtName.setText(student.getName());
		String sex = student.getSex();// 性别
		for (int i = 0; i < rdbSex.length; i++) {
			if (sex.equals(rdbSex[i].getText())) {
				rdbSex[i].setSelected(true);
				break;
			}
		}
		cboMinZu.setSelectedItem(student.getNation());
		txtPhone.setText(student.getTelephone());
		txtBirthday.setText(f1.format(student.getBirthday()));
		txtAddress.setText(student.getAddress());
		txtJianLi.setText(student.getReume());
		String happy = student.getHappy();
		if (happy != null) {
			for (int i = 0; i < chkHappy.length; i++) {
				String str = chkHappy[i].getText();
				if (happy.indexOf(str) != -1) {
					chkHappy[i].setSelected(true);
				}
			}
		}
		// 显示个人照片
		String path = student.getPhotoPath();
		if (path != null) {
			File f = new File(path);
			if (f.exists()) {
				ImageIcon icon = new ImageIcon(path);
				// 图片自适应大小
				icon.setImage(icon.getImage().getScaledInstance(
						lblPicture.getWidth(), lblPicture.getHeight(),
						Image.SCALE_DEFAULT));
				lblPicture.setIcon(icon);
				lblPicture.updateUI();
			}
		}
	}

	/**
	 * 上传图片
	 * 
	 * @param fileName上传图片文件名
	 */
	private void Savepoint(String photoPath) {
		// 创建文件对象
		File f = new File(photoPath);
		if (f.exists()) {
			// 控制文件大小,不超过15KB
			long size = f.length();
			if (size > 50 * 1024) {
				JOptionPane.showMessageDialog(this, "上传图片不能超过50KB");
				return;
			}
			// 使用文件输入，输出流进行上传图片
			String str = UUID.randomUUID().toString();
			// 获取长传文件的扩展名
			String extension = FilenameUtils.getExtension(photoPath);
			String savePath = "images/";
			// 判断路径是否存在,如果不存在就创建
			File f1 = new File(savePath);
			if (!f1.isDirectory()) {
				f1.mkdirs();
			}
			try {
				FileInputStream fis = new FileInputStream(photoPath);
				FileOutputStream fos = new FileOutputStream(savePath + str
						+ "." + extension);
				byte[] b = new byte[fis.available()];// 获取输入流中可用的字节数
				int length = 0;
				while ((length = fis.read(b)) > 0) {
					fos.write(b, 0, length);
				}
				fis.close();
				fos.close();

			} catch (Exception e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(this, "上传图有误");
				return;
			}
			// 显示长传的图片

			ImageIcon icon = new ImageIcon(savePath + str + "." + extension);
			// 图片自适应大小
			icon.setImage(icon.getImage().getScaledInstance(
					lblPicture.getWidth(), lblPicture.getHeight(),
					Image.SCALE_DEFAULT));
			lblPicture.setIcon(icon);
			lblPicture.updateUI();
			// 最后根系数据库中图片路径字段内容
			StudentSqlDao sDao = new StudentSqlDao();
			sDao.updateUserPhotoPath(Integer.parseInt(txtNo.getText()),
					savePath + str + "." + extension);

		}

	}

}
